package com.blue.code.ftl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.blue.code.bean.EntityBean;

import freemarker.template.Configuration;
import freemarker.template.Template;

@Component
public class ResponseHandler implements Handler
{
	private static Logger logger = LoggerFactory.getLogger(ResponseHandler.class);
	
	@Value("${ftl.response}")
	private String response;
	
	private Template template;
	
	public ResponseHandler()
	{
	}

	@Override
	public void handle(EntityBean bean, String output, String pkg) throws Exception
	{
		Map<String, Object> root = new HashMap<>();
		root.put("pkg", pkg + "." + response);
		root.put("clazz", bean.getClazz());
		root.put("name", bean.getName());
		root.put("idList", bean.getIdList());
		root.put("columnList", bean.getColumnList());
		
		String clazz = bean.getClazz() + "Response";
		String fileName = this.output(template, root, output, "message", clazz);
		logger.info("输出文件：{}", fileName);
		
	}

	@Override
	public void initConfiguration(Configuration conf, String ext) throws Exception
	{
		String ftl = response + ext;
		template = conf.getTemplate(ftl);
		logger.info("初始化 Response 模板：{}", ftl);
		
	}
}
